import axios from 'axios'
export default {
  state () {
    return {
      list: []
    }
  },
  mutations: {
    changlist (state, list) {
      state.list = list
    }
  },
  actions: {
    //获取购物车
    async getCartList ({ commit }) {
      const res = await axios('http://localhost:8000/cart')
      console.log(res);
      commit('changlist', res.data)
    },
    //点击修改数量
    async changeCount (store, obj) {
      const res = await axios.patch('http://localhost:8000/cart/' + obj.id, {
        count: obj.count
      })
      // console.log(store);
      console.log(res);
      store.dispatch('getCartList')
    }
  },
  getters: {
    totle (state) {
      return state.list.reduce((pre, next) => pre + next.count, 0)
    },
    totlePrice (state) {
      return state.list.reduce((pre, next) => pre + next.price * next.count, 0)
    }

  },
  namespaced: true
}